package com.imay.live.helper;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.imay.live.R;
import com.imay.live.activity.PermissionsGrantActivity;
import com.imay.live.activity.RoomBaseActivity;
import com.imay.live.proto.helper.DefaultDeniedPermissionHandler;
import com.imay.live.utils.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;


/**
 * 分享helper
 */
public class ShareSDKHelper {
    private final static int duration = 200;

    /**
     * 获取H5跳转返回字段
     *
     * @param intent
     * @return
     */
    public static String getDataUrl(Intent intent) {
        Uri uri = intent.getData();
        if (H5Helper.isAppSchemeUri(uri)) {
            return uri.toString();
        }
        return null;
    }

    /**
     * 获取建造者
     * UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
     *
     * @param activity
     * @param sharetype 1、WEB，2、IMG, 3、TEXT
     * @param media     分享平台
     * @return {@link Builder}
     */
    public static Builder getBuilder(Activity activity, TYPE sharetype, SHARE_MEDIA media) {
        com.umeng.socialize.utils.Log.LOG = true;
        return new Builder(activity, sharetype, media);
    }

    public static Builder getBuilder(Activity activity, SHARE_MEDIA media) {
        com.umeng.socialize.utils.Log.LOG = true;
        return new Builder(activity, TYPE.WEB, media);
    }

    public static Builder getBuilder(Activity activity, SHARE_MEDIA media, UMShareListener umShareListener) {
        return new Builder(activity, TYPE.WEB, media, umShareListener);
    }

    public enum TYPE {
        WEB, IMG, TEXT
    }

    public static class Builder {
        // 转发类型
        private TYPE sharetype;

        private Activity activity;

        // 转发平台
        private SHARE_MEDIA platform;

        private ShareAction mBuilder;

        // 链接
        private String shareLink;

        // 标题
        private String title;

        // 网络图片链接
        private String imgUrl;

        // 图片文件
        private File imgFile;

        // 图片资源
        private int imgRid;

        // 描述
        private String des;


        private UMShareListener listener;


        public Builder(Activity activity, TYPE sharetype, SHARE_MEDIA media) {
            mBuilder = new ShareAction(activity);
            this.activity = activity;
            this.sharetype = sharetype;
            this.platform = media;
            listener = new DefaultListener(activity);
        }

        public Builder(Activity activity, TYPE sharetype, SHARE_MEDIA media, UMShareListener umShareListener) {
            mBuilder = new ShareAction(activity);
            this.activity = activity;
            this.sharetype = sharetype;
            this.platform = media;
            listener = umShareListener;
        }

        public Builder setShareLink(String shareLink) {
            this.shareLink = shareLink;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setDes(String des) {
            this.des = des;
            return this;
        }

        public Builder setImgUrl(String imgUrl) {
            this.imgUrl = imgUrl;
            return this;
        }

        public Builder setImgFile(File imgFile) {
            this.imgFile = imgFile;
            return this;
        }

        public Builder setImgRid(int imgRid) {
            this.imgRid = imgRid;
            return this;
        }

        public void share() {
            share(null);
        }

        public void share(UMShareListener mlistener) {
            if (mlistener == null) {
                mlistener = listener;
            }


            // 缩略图
            UMImage thumb = new UMImage(activity, R.mipmap.app_launcher);
            if (!TextUtils.isEmpty(imgUrl)) {
                thumb = new UMImage(activity, imgUrl);
            }
            if (imgRid != 0) {
                thumb = new UMImage(activity, imgRid);
            }
            if (imgFile != null) {
                thumb = new UMImage(activity, imgFile);
            }


            switch (sharetype) {
                case WEB:
                    if (TextUtils.isEmpty(shareLink)) {
                        return;
                    }
                    UMWeb web = new UMWeb(shareLink);
                    if (!TextUtils.isEmpty(des))
                        web.setDescription(des);
                    if (!TextUtils.isEmpty(title))
                        web.setTitle(title);
                    web.setThumb(thumb);
                    mBuilder.withMedia(web).setPlatform(platform).setCallback(mlistener).share();
                    break;
                case TEXT:
                    if (TextUtils.isEmpty(des)) {
                        return;
                    }
                    mBuilder.withText(des).setPlatform(platform).setCallback(mlistener).share();
                    break;
                case IMG:
                    UMImage pic = null;
                    if (!TextUtils.isEmpty(imgUrl)) {
                        pic = new UMImage(activity, imgUrl);
                    }
                    if (imgRid != 0) {
                        pic = new UMImage(activity, imgRid);
                    }
                    if (imgFile != null) {
                        pic = new UMImage(activity, imgFile);
                    }
                    if (pic == null) {
                        return;
                    }
                    pic.setThumb(thumb);
                    if (TextUtils.isEmpty(des)) {
                        mBuilder.withMedia(pic).setPlatform(platform).setCallback(mlistener).share();
                    } else {
                        pic.setDescription(des);
                        pic.setTitle(des);
                        mBuilder.withMedia(pic).setPlatform(platform).setCallback(mlistener).withText(des).share();
                    }
                    break;
            }
        }

        public void share(final String url, final String title, final String des, final String imgUrl) {
            if (platform == SHARE_MEDIA.QZONE || platform == SHARE_MEDIA.QQ) {
                PermissionsGrantActivity.grantPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}
                        , new DefaultDeniedPermissionHandler(activity) {
                            @Override
                            public void onPermissionsResult(String[] grantedpermissions, String[] denied_permissions) {
                                setShareLink(url).setTitle(title).setDes(des).setImgUrl(imgUrl);
                                share(listener);
                            }

                            @Override
                            protected void onDialogChoice(int choice) {
                            }
                        });
            } else {
                setShareLink(url).setTitle(title).setDes(des).setImgUrl(imgUrl);
                share(listener);
            }

        }
    }

    /**
     * 公共回调
     */
    private static class DefaultListener implements UMShareListener {

        private Activity activity;

        public DefaultListener(Activity context) {
            activity = context;
        }

        @Override
        public void onStart(SHARE_MEDIA share_media) {
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            ShareNoticeHelper.getInstance().shareDelayed();
            if (activity instanceof ShareCallBack) {
                ((RoomBaseActivity) activity).onSuccessCallBack(activity.getString(R.string.str_dynamics_share_result_success));
            } else {
                ToastUtil.showImg(activity, activity.getString(R.string.str_dynamics_share_result_success), R.mipmap.jiaohutishidagou_3x, duration);
            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            ShareNoticeHelper.getInstance().shareDelayed();
            String message = "";
            if (throwable.getMessage().contains("2008")) {
                message = activity.getString(R.string.str_dynamics_share_notinstall);
            } else if (throwable.getMessage().contains("2000")) {
                message = activity.getString(R.string.str_dynamics_share_result_error);
            } else {
                message = activity.getString(R.string.str_dynamics_share_result_error);
            }
            if (activity instanceof ShareCallBack) {
                ((RoomBaseActivity) activity).onFailCallBack(message);
            } else {
                ToastUtil.showImg(activity, message, R.mipmap.jiaohutishidaxia_3x, duration);
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            ShareNoticeHelper.getInstance().shareDelayed();
            if (activity instanceof RoomBaseActivity) {
                ((RoomBaseActivity) activity).onFailCallBack(activity.getString(R.string.str_dynamics_share_result_cancel));
            } else {
                ToastUtil.showImg(activity, activity.getString(R.string.str_dynamics_share_result_cancel), R.mipmap.jiaohutishidaxia_3x, duration);
            }
        }
    }

    public interface ShareCallBack {
        void onFailCallBack(String message);

        void onSuccessCallBack(String message);
    }
}
